/**
 * 商户路由管理初始化
 */
var MchRouter = {
    id: "MchRouterTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
MchRouter.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        	{title: '商户号', field: 'mchNo', visible: true, align: 'center', valign: 'middle'},
            {title: '商户名', field: 'mchName', visible: true, align: 'center', valign: 'middle'},
            {title: '方式编码', field: 'payWayCode', visible: true, align: 'center', valign: 'middle'},
            {title: '方式名称', field: 'payWayName', visible: true, align: 'center', valign: 'middle'},
            {title: '渠道编码', field: 'payChannelCode', visible: true, align: 'center', valign: 'middle'},
            {title: '渠道名称', field: 'payChannelName', visible: true, align: 'center', valign: 'middle'},
            {title: '签约费率', field: 'signRate', visible: true, align: 'center', valign: 'middle'},
            {title: '信用卡限制', field: 'limitCredit', visible: true, align: 'center', valign: 'middle'},
            {title: '扣费类型', field: 'chargeType', visible: true, align: 'center', valign: 'middle',formatter:"chargeTypeFormatter"},
            {title: '扣费金额', field: 'chargeAmount', visible: true, align: 'center', valign: 'middle'},
            {title: '状态', field: 'status', visible: true, align: 'center', valign: 'middle',formatter:"statusFormatter"},
            {title: '创建时间', field: 'cerateTime', visible: true, align: 'center', valign: 'middle'}
           
    ];
};


function statusFormatter(value, row, index){
	if (value == 1) {
		return '<span class="label label-success">启用</span>';
	} else {
		return '<span class="label label-default">停用</span>';
	}
}

function chargeTypeFormatter(value, row, index){
	if (value == 1) {
		return '<span class="label label-success">用户</span>';
	} else {
		return '<span class="label label-default">商户</span>';
	}
}



/**
 * 检查是否选中
 */
MchRouter.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        MchRouter.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加商户路由
 */
MchRouter.openAddMchRouter = function () {
    var index = layer.open({
        type: 2,
        title: '添加商户路由',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/mchRouter/mchRouter_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看商户路由详情
 */
MchRouter.openMchRouterDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '商户路由详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/mchRouter/mchRouter_update/' + MchRouter.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除商户路由
 */
MchRouter.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/mchRouter/delete", function (data) {
            Feng.success("删除成功!");
            MchRouter.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("mchRouterId",this.seItem.id);
        ajax.start();
    }
};

/**
 * 查询商户路由列表
 */
MchRouter.search = function () {
    var queryData = {};
    queryData['mchNo'] = $("#mchNo").val();
    queryData['payWayCode'] = $("#payWayCode").val();
    queryData['payChannelCode'] = $("#payChannelCode").val();
    queryData['status'] = $("#status").val();    
    
    MchRouter.table.refresh({query: queryData});
};

$(function () {
    var defaultColunms = MchRouter.initColumn();
    var table = new BSTable(MchRouter.id, "/mchRouter/list", defaultColunms);
    table.setPaginationType("client");
    MchRouter.table = table.init();
});
